package com.example.yang_app_sixth_server;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText input;
    TextView show;
    // 定义界面上的一个按钮
    Button send,con;
    OutputStream os;
    Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input=findViewById(R.id.input);
        send=findViewById(R.id.send);
        show=findViewById(R.id.show);
        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg=new Message();
                msg.what=0x1234;
                Bundle bundle=new Bundle();
                bundle.putString("send_content",input.getText().toString()+"\n");
                msg.setData(bundle);
                ClientThread.SendHandler.sendMessage(msg);
            }
        });
        con=findViewById(R.id.con);
        con.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    new Thread(new ClientThread(handler)).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x1234){
                    show.setText(show.getText().toString()+msg.getData().getString("receive_content"));
                }
            }
        };
        try {
            new Thread(new ClientThread(handler)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

}
